<?php
namespace App\Common\Lib;

use App\Common as com;
use App\Common\Lib\Debug as nameDebug;
use Hyperf\Config\Config;

class Debug extends com\Lib\LibBase {

	/**
	 * 调试输出
	 * @param mixed $var
	 * @param bool $isEcho [true][默认]
	 * @return mixed
	 */
	public static function d( $var = '{NO_INPUT}', bool $isEcho = true ){

		if( $var == '{NO_INPUT}' ){
			$echoData = $var;
		}else{
			//
			if( is_numeric( $var ) && $var < -10000000 ){
				$var = [
					'code' => $var,
					'str' => com\Cla\Err\Code::getText( $var ),
					'remark' => "自动转换代码",
				];
			}
			//
			$echoData = com\Lib\Think\Debug::dump( $var, false );
		}
		//
		if( $isEcho ){
			echo $echoData;
		}
		//
		return $echoData;
	}
	
	/**
	 * 调试输出并结束程序
	 * @param mixed $var
	 * @param bool $isEcho
	 * @return mixed
	 */
/*	public static function de( $var = '{NO_INPUT}', $isEcho = 0 ){
		return self::d( $var, $is_echo );
//		exit();
	}*/


	/**
	 * 是否为调试模式
	 * @return bool
	 */
	public static function isDebug(  ):bool {

		return config( 'web.debugInfo.isDebug' );
	}

	/**
	 * 是否为测试模式
	 * @return bool
	 */
	public static function isTest(  ):bool {
		return config( 'web.debugInfo.isTest' );
	}

	/**
	 * 是否为服务器运行环境
	 * @return bool
	 */
	public static function runEnvIsServer(  ):bool {
		return config( 'web.debugInfo.runEnvId' ) === 1;
	}

	/**
	 * 是否为本地运行环境
	 * @return bool
	 */
	public static function runEnvIsLocal(  ):bool {
		return config( 'web.debugInfo.runEnvId' ) === 0;
	}

	/**
	 * 获取运行环境id
	 * @return int
	 */
	public static function getRunEnvId(  ):int {
		return config( 'web.debugInfo.runEnvId' );
	}

	/**
	 * @return nameDebug\Log
	 */
	public static function newLog(  ):nameDebug\Log {
		return new nameDebug\Log();
	}

	/**
	 * 打印调用过程
	 * @param int|null $maxTreeNum 最大链路数
	 */
	public static function showDebugBacktrace( ?int $maxTreeNum = null ):void {
		d( self::getDebugBacktrace( $maxTreeNum ) );
	}

	/**
	 * 获取调用过程
	 * @param int|null $maxTreeNum 最大链路数
	 * @return array
	 */
	public static function getDebugBacktrace( ?int $maxTreeNum = null ):array {
		$debugBacktrace = debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS, $maxTreeNum );
		$showInfo = [];
		foreach( $debugBacktrace as $debugBacktraceValue ){
			$debugBacktraceValue['line'] = $debugBacktraceValue['line'] ?? '-';
			$debugBacktraceValue['file'] = $debugBacktraceValue['file'] ?? '-';
			$debugBacktraceValue['function'] = $debugBacktraceValue['function'] ?? '-';
			$showInfo[] = "[{$debugBacktraceValue['line']}]{$debugBacktraceValue['file']}->{$debugBacktraceValue['function']}()";
		}
		//
		return $showInfo;
	}

}